define(['app','pagination','autocomplete','datetime','editor'],function(app){
  'user strict';
  /**
   *  员工管理
   */
  app.controller('Document',function($scope, dataFactoryCRM, $q,$compile,$http,$timeout){

    //  初始化所有显示
    $scope.sexType = [{k:'0',v:'未知'},{k:'1',v:'男'},{k:'2',v:'女'},{k:'3',v:'男'}];
    $scope.minzuType = [{k:'0',v:'汉族'},{k:'1',v:'蒙古族'},{k:'2',v:'回族'},{k:'3',v:'满族'},{k:'4',v:'维吾尔族'},{k:'5',v:'壮族'},{k:'6',v:'朝鲜族'}];
    $scope.zzmmType = [{k:'0',v:'未知'},{k:'1',v:'党员'},{k:'2',v:'预备党员'},{k:'3',v:'团员'},{k:'4',v:'群众'}];
    $scope.marryType = [{k:'0',v:'未婚'},{k:'1',v:'已婚'}];
    $scope.familyType = [{k:'0',v:'父亲'},{k:'1',v:'母亲'},{k:'2',v:'子女'}];
    $scope.xlType = [{k:'0',v:'未知'},{k:'1',v:'博士研究生'},{k:'2',v:'硕士研究生'},{k:'3',v:'本科'},{k:'4',v:'大专'},{k:'5',v:'中专及以下'}];
    $scope.ustate = [{k:'0',v:'禁止登陆'},{k:'1',v:'试用员工'},{k:'2',v:'正式员工'}];
    $scope.statusType = [{k:'0',v:'正常'},{k:'1',v:'产假'},{k:'2',v:'停薪留职'}];
    //惩罚缘由
    $scope.cfcause = [{ k: '-1', v: '全部' }, { k: '0', v: '迟到' }, { k: '1', v: '早退' }, { k: '2', v: '其他' }];
    //惩罚方式
    $scope.cfways = [{ k: '-1', v: '全部' }, { k: '0', v: '现金' }, { k: '1', v: '加班' }, { k: '2', v: '其他' }];
    //嘉奖缘由
    $scope.jjcause = [{ k: '-1', v: '全部' }, { k: '0', v: '销售精英' }, { k: '1', v: '全勤奖' }, { k: '2', v: '月冠金奖' }];
    //嘉奖方式
    $scope.jjways = [{ k: '-1', v: '全部' }, { k: '0', v: '现金' }, { k: '1', v: '伙食补助' }, { k: '2', v: '房补' }];
    $scope.findex = 'false';
    $scope.familys = [];
    $scope.family = {};
    $scope.eindex = 'false';
    $scope.edus = [];
    $scope.edu = '';
    $scope.tindex = 'false';
    $scope.trains = [];
    $scope.train = '';
    $scope.windex = 'false';
    $scope.works = [];
    $scope.work = '';

    //个人信息
    $scope.myInfo = function() {
      $scope.uid = $scope.user.user_id;
      dataFactoryCRM.getDataByAPI('Document/index?user_id='+$scope.uid)
        .then(function(_d) {
          $scope.user = _d.data['user'];
          $scope.userExt = _d.data['userExt'];
          $scope.postExch = _d.data['postExch'];
          $scope.trainCom = _d.data['trainCom'];
          $scope.commend = _d.data['commend'];
          $scope.punish = _d.data['punish'];
          $scope.offer = _d.data['offer'];
        });

    }
    $scope.myInfo();

    // 家庭信息操作
    $scope.familyAdd = function(){
      setTimeout(function() { $('.ui.table.family .ui.dropdown').dropdown() }, 50);
      $('.ui.table.family').transition('fade down');
      $('.fr.family .ui.button').transition('toggle');
    }
    $scope.familySave = function(){
      if ($('.fam.ui.form').form('is valid')){
        $scope.findex != 'false' ? $scope.familys.splice($scope.findex, 1, $scope.family) : $scope.familys.push($scope.family);
        $scope.findex = 'false';
        $scope.family = '';
        $('#famFrom').form('clear');
        $('.ui.table.family').transition('fade down');
        $('.fr.family .ui.button').transition('toggle');
      }
    }
    $scope.familyCancel = function(){
      $scope.findex = 'false';
      $scope.family = '';
      $('#famFrom').form('clear');
      $('.ui.table.family').transition('fade down');
      $('.fr.family .ui.button').transition('toggle');
    }
    // 员工家庭修改
    $scope.familyEdit = function(){
      $scope.family = angular.copy(this.vfamily);
      $scope.findex = this.$index;
      setTimeout(function() { $('.ui.table.family .ui.dropdown').dropdown() }, 50);
      $('.ui.table.family').transition('fade down');
      $('.fr.family .ui.button').transition('toggle');
    }
    // 员工家庭信息删除
    $scope.familyDel = function(){
      $scope.findex = this.$index;
      $scope.familys.splice($scope.findex, 1);
      $scope.findex = 'false';
    }

    // 员工教育经历操作
    $scope.eduAdd = function(){
      setTimeout(function() { $('.ui.table.edu .ui.dropdown').dropdown() }, 50);
      $('.ui.table.edu').transition('fade down');
      $('.fr.edu .ui.button').transition('toggle');
    }
    $scope.eduSave = function(){
      if ($('.edu.ui.form').form('is valid')){
        $scope.eindex != 'false' ? $scope.edus.splice($scope.eindex, 1, $scope.edu) : $scope.edus.push($scope.edu);
        $scope.eindex = 'false';
        $scope.edu = '';
        $('#eduFrom').form('clear');
        $('.ui.table.edu').transition('fade down');
        $('.fr.edu .ui.button').transition('toggle');
      }
    }
    $scope.eduCancel = function(){
      $scope.eindex = 'false';
      $scope.edu = '';
      $('#eduFrom').form('clear');
      $('.ui.table.edu').transition('fade down');
      $('.fr.edu .ui.button').transition('toggle');
    }
    $scope.eduEdit = function(){
      $scope.edu = angular.copy(this.vedu);
      $scope.eindex = this.$index;
      setTimeout(function() { $('.ui.table.edu .ui.dropdown').dropdown() }, 50);
      $('.ui.table.edu').transition('fade down');
      $('.fr.edu .ui.button').transition('toggle');
    }
    $scope.eduDel = function(){
      $scope.eindex = this.$index;
      $scope.edus.splice($scope.eindex, 1);
      $scope.eindex = 'false';
    }

    // 员工培训经历操作
    $scope.trainAdd = function(){
      setTimeout(function() { $('.ui.table.train .ui.dropdown').dropdown() }, 50);
      $('.ui.table.train').transition('fade down');
      $('.fr.train .ui.button').transition('toggle');
    }
    $scope.trainSave = function(){
      if ($('.train.ui.form').form('is valid')){
        $scope.tindex != 'false' ? $scope.trains.splice($scope.tindex, 1, $scope.train) : $scope.trains.push($scope.train);
        $scope.tindex = 'false';
        $scope.train = '';
        $('#trainFrom').form('clear');
        $('.ui.table.train').transition('fade down');
        $('.fr.train .ui.button').transition('toggle');
      }
    }
    $scope.trainCancel = function(){
      $scope.tindex = 'false';
      $scope.train = '';
      $('#trainFrom').form('clear');
      $('.ui.table.train').transition('fade down');
      $('.fr.train .ui.button').transition('toggle');
    }
    $scope.trainEdit = function(){
      $scope.train = angular.copy(this.vtrain);
      $scope.tindex = this.$index;
      setTimeout(function() { $('.ui.table.train .ui.dropdown').dropdown() }, 50);
      $('.ui.table.train').transition('fade down');
      $('.fr.train .ui.button').transition('toggle');
    }
    $scope.trainDel = function(){
      $scope.tindex = this.$index;
      $scope.trains.splice($scope.tindex, 1);
      $scope.tindex = 'false';
    }


    // 员工工作经历操作
    $scope.workAdd = function(){
      setTimeout(function() { $('.ui.table.work .ui.dropdown').dropdown() }, 50);
      $('.ui.table.work').transition('fade down');
      $('.fr.work .ui.button').transition('toggle');
    }
    $scope.workSave = function(){
      if ($('.work.ui.form').form('is valid')){
        $scope.windex != 'false' ? $scope.works.splice($scope.windex, 1, $scope.work) : $scope.works.push($scope.work);
        $scope.windex = 'false';
        $scope.work = '';
        $('#workFrom').form('clear');
        $('.ui.table.work').transition('fade down');
        $('.fr.work .ui.button').transition('toggle');
      }
    }
    $scope.workCancel = function(){
      $scope.windex = 'false';
      $scope.work = '';
      $('#workFrom').form('clear');
      $('.ui.table.work').transition('fade down');
      $('.fr.work .ui.button').transition('toggle');
    }
    $scope.workEdit = function(){
      $scope.work = angular.copy(this.vwork);
      $scope.windex = this.$index;
      setTimeout(function() { $('.ui.table.work .ui.dropdown').dropdown() }, 50);
      $('.ui.table.work').transition('fade down');
      $('.fr.work .ui.button').transition('toggle');
    }
    $scope.workDel = function(){
      $scope.windex = this.$index;
      $scope.works.splice($scope.windex, 1);
      $scope.windex = 'false';
    }

    $scope.showEmlModal = function(user_id){
      dataFactoryCRM.getDataByAPI('document/getEditInfo?user_id='+$scope.uid)
          .then(function(d){
            if(d&&d.rcode){
              $timeout(function() {
                $scope.editUser = d.data['user'];
                $scope.editEmpl = d.data['userExt'];
                $scope.familys = d.data['family'];
                $scope.edus = d.data['education'];
                $scope.trains = d.data['train'];
                $scope.works = d.data['workexp'];
              });
              $('.ui.show').modal({
                  autofocus:false,
                  onApprove: function() {
                          return true;
                            }
                  }).modal('setting', 'closable', false).modal('show');
            }else{
              $scope.api_error('接口请求失败');
            }
          });
    }

    $scope.api_error = function ( msg ) {
      $('.modal.modal2').remove();
      $('<div class="ui small modal modal2" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge red warning outline icon"></i>' +
          '<p style="padding-top: 15px;">'+msg+'</p>' +'</div></div>'
      ).modal('show');
    };

    $scope.api_success = function( msg ){
        $('.modal.modal1').remove();
        $('<div class="ui small modal modal1" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge green check circle outline icon"></i>' +
                '<p style="padding-top: 15px;">'+msg+'</p>' +'</div></div>'
            ).modal('show');
    };

    $('.content .menu .item').tab();
    // $('.edit .menu .item').tab();

    //员工修改弹出层
    $scope.editEmlModal = function( user_id){
      function editEml( allData ){
        dataFactoryCRM.postDataByAPI('document/editEmpl',allData)
          .then(function(d){
            $scope.$emit('NOTBUSY');
            if(d.rcode){
              $scope.myInfo();
              $scope.api_success(d.msg);
              return true;
            }else{
              $scope.api_error(d.msg);
              return false;
            }
          });
      }

      dataFactoryCRM.getDataByAPI('document/getEditInfo?user_id='+$scope.uid)
        .then(function(d){
          if(d&&d.rcode){
            $timeout(function() {
              $scope.editUser = d.data['user'];
              $scope.editEmpl = d.data['userExt'];
              $scope.familys = d.data['family'];
              $scope.edus = d.data['education'];
              $scope.trains = d.data['train'];
              $scope.works = d.data['workexp'];
              $scope.editEmpl.ext_lastPer = $scope.uid;
              
              $scope.allData = {
                user : $scope.editUser,
                empl : $scope.editEmpl,
                family : $scope.familys,
                edu : $scope.edus,
                train : $scope.trains,
                work : $scope.works
              };
              setTimeout(function() { $('.fir.ui.form .ui.dropdown').dropdown() }, 50);
            });
            $('.fir.ui.form').form('clean');
            $scope.editEmpl='';
            $('.ui.edit').modal({
                autofocus:false,
                onApprove:function(){
                    if($('.fir.ui.form').form('is valid')) {
                        return editEml($scope.allData);
                    }else{
                        return false;
                    }
                  }
                }).modal('setting', 'closable', false).modal('show');
          }else{
            $scope.api_error('接口请求失败');
          }
        });
    }
      // 员工修改

      window.loadClientCacheToScope($scope, function(){

      });
  });
});
